<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Home Page</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }
        h1 {
            text-align: center;
            color: #333;
            margin-top: 20px;
        }
        table {
            width: 80%;
            margin: 20px auto;
            border-collapse: collapse;
            background: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        th, td {
            padding: 12px;
            border: 1px solid #ddd;
            text-align: center;
        }
        th {
            background-color: #f8f8f8;
            color: #333;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #f1f1f1;
        }
        .status-enabled, .status-disabled {
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .status-enabled .status-indicator {
            background-color: green;
        }
        .status-disabled .status-indicator {
            background-color: red;
        }
        .status-indicator {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin-right: 8px;
        }
        .action-btn, .update-btn {
            padding: 8px 12px;
            margin: 0 5px;
            border: none;
            cursor: pointer;
            font-size: 14px;
            color: #fff;
            border-radius: 4px;
            transition: background-color 0.3s ease;
        }
        .action-btn {
            background-color: #007bff;
        }
        .action-btn:hover {
            background-color: #0056b3;
        }
        .update-btn {
            background-color: #dc3545;
        }
        .update-btn:hover {
            background-color: #c82333;
        }
    </style>
    <script>
        function toggleSelectAll() {
            var selectAllCheckbox = document.getElementById('selectAll');
            var checkboxes = document.getElementsByClassName('selectItem');
            for (var i = 0; i < checkboxes.length; i++) {
                checkboxes[i].checked = selectAllCheckbox.checked;
            }
        }

        function changeUserStatus(id, identity) {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "${pageContext.request.contextPath}/EnabledAndDisabled", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    // 更新状态文本
                    var statusElement = document.getElementById("status-" + id);
                    statusElement.innerHTML = '<span class="status-indicator"></span>' + (identity == 1 ? "启用" : "禁用");
                    statusElement.className = identity == 1 ? "status-enabled" : "status-disabled";

                    // 更新按钮文本和点击事件
                    var btn = document.getElementById("btn-" + id);
                    btn.innerText = identity == 1 ? "禁用" : "启用";
                    btn.setAttribute("onclick", "changeUserStatus(" + id + ", " + (identity == 1 ? 0 : 1) + ")");
                }
            };
            xhr.send("id=" + id + "&identity=" + identity);
        }
    </script>
</head>
<body>

<h1>账号管理</h1>

<form id="bulkDeleteForm" action="${pageContext.request.contextPath}/delete" method="post" onsubmit="return confirm('确定要批量删除所选用户吗？');">
    <table>
        <thead>
        <tr>
            <th>用户名</th>
            <th>密码</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach var="user" items="${applicationScope.userList}">
            <tr>
                <td>${user.username}</td>
                <td>${user.password}</td>
                <td id="status-${user.id}" class="<c:choose>
                            <c:when test="${user.identity == 1}">status-enabled</c:when>
                            <c:otherwise>status-disabled</c:otherwise>
                        </c:choose>">
                    <span class="status-indicator"></span>
                    <c:choose>
                        <c:when test="${user.identity == 1}">启用</c:when>
                        <c:otherwise>禁用</c:otherwise>
                    </c:choose>
                </td>
                <td>
                    <button type="button" id="btn-${user.id}" class="action-btn" onclick="changeUserStatus(${user.id}, ${user.identity == 1 ? 0 : 1})">
                        <c:choose>
                            <c:when test="${user.identity == 1}">禁用</c:when>
                            <c:otherwise>启用</c:otherwise>
                        </c:choose>
                    </button>
                    <button type="button" class="update-btn" onclick="updateUser(${user.id})">删除</button>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</form>

</body>
</html>
